package com.example.alipay_10_3.unionpaysdk;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * Created by pridas on 2020/3/24.
 */
public class AcpDemoPage extends HttpServlet{

    /**
	 * 
	 */
	private static final long serialVersionUID = -3224996852181358949L;
	public final static String key_neededParams = "neededParams";
    public final static String key_demoName = "demoName";
    public final static String key_faq = "faq";
    public final static String key_postUrl = "postUrl";
    public final static String key_addnInfo = "addnInfo";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String demoName = req.getParameter(key_demoName);
        req.setAttribute("postUrl", req.getRequestURI() + "?" + key_demoName + "=" + demoName);

        req.setAttribute(key_demoName, demoName);

        try {
            Class<?> c = Class.forName(demoName);
            try{
	            req.setAttribute(key_faq, (String) c.getMethod("getFaq").invoke(null));
            } catch (NoSuchMethodException e) { }
            try{
	            req.setAttribute(key_neededParams, (String) c.getMethod("neededParams").invoke(null));
            } catch (NoSuchMethodException e) { }
            try{
	            req.setAttribute(key_addnInfo, (String) c.getMethod("getAddnInfo").invoke(null));
            } catch (NoSuchMethodException e) { }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        req.getRequestDispatcher("pages/demoParamPage.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String demoName = req.getParameter(key_demoName);

        HashMap<String, String> params = new HashMap<String, String>();
        Enumeration<String> enumeration = req.getParameterNames();
        while(enumeration.hasMoreElements()){
            String key = enumeration.nextElement();
            params.put(key, req.getParameter(key));
        }
        params.remove(key_demoName);

        try {
            Class<?> c = Class.forName(demoName);
            Constructor<?> constructor = c.getConstructor();
            AcpDemo demo = (AcpDemo) constructor.newInstance();
            demo.testDemo(params);
            String resultStr = demo.getResultStr();
            if(demo.resultNeedHtmlEncode()){
                resp.getWriter().write(DemoUtil.escapeHtmlAndReplaceLfCr(resultStr));
            } else {
                resp.getWriter().write(resultStr);
            }
            return;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        resp.getWriter().write("没取到demo……");
    }
}
